package com.example.dhp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.example.dhp.R;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_EXTERNAL_STORAGE = 1;

    private static String[] PERMISSIONS_STORAGE = {

            "android.permission.READ_EXTERNAL_STORAGE",

            "android.permission.WRITE_EXTERNAL_STORAGE" };

    private static final int REQUEST_MEDIA_PROJECTION = 0x01;

    public static void verifyStoragePermissions(Activity activity) {
        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
        if (permission != PackageManager.PERMISSION_GRANTED) {
            // 没有写的权限，去申请写的权限，会弹出对话框
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
 }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        verifyStoragePermissions(this);
        Button button=findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
            }
        });


        String passSpu = SPUtil.get(MainActivity.this,"pass","") + "";

        EditText pass =(EditText)findViewById(R.id.pass);
        if(!"".equals(passSpu)){
            pass.setText(passSpu);
        }else{
            pass.setText(":9012");
        }


        EditText clubId =(EditText)findViewById(R.id.clubId);
        clubId.setText(SPUtil.get(MainActivity.this,"clubId","") + "");

        button=findViewById(R.id.button3);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                EditText clubId =(EditText)findViewById(R.id.clubId);
                SPUtil.putString(MainActivity.this,"clubId",clubId.getText().toString());

                EditText pass =(EditText)findViewById(R.id.pass);
                SPUtil.putString(MainActivity.this,"pass",pass.getText().toString());

                Common.clubId = clubId.getText().toString();
                Common.url =   "http://" + pass.getText().toString() ;
                if(Common.clubId.equals("")){
                    Toast.makeText(MainActivity.this, "填写俱乐部id："+Common.clubId+"，"+Common.url, Toast.LENGTH_SHORT).show();
                }else{
                    Common.param.put("id",Common.clubId);
                    Toast.makeText(MainActivity.this, "绑定成功："+Common.clubId+"，"+Common.url, Toast.LENGTH_SHORT).show();
                    startActivity(getPackageManager().getLaunchIntentForPackage("com.dahuopin.zhichat"));
                }

            }
        });

    }
}
